package org.bing.engine.console.service;

import org.bing.engine.common.message.Message;
import org.bing.engine.common.message.Messenger;
import org.bing.engine.common.remoting.RemotingProxyFactory;
import org.bing.engine.core.domain.Controller;
import org.bing.engine.utility.helper.StringHelper;

public class SmartMessenger {
    private RemotingProxyFactory proxyFactory;
    private String urlTemplate;

    public void setProxyFactory(RemotingProxyFactory factory) {
        this.proxyFactory = factory;
    }

    public void setUrlTemplate(String template) {
        this.urlTemplate = template;
    }

    public Object send(Controller controller, Message msg) {
        String url = StringHelper.replaceWithProperty(urlTemplate, "host", controller.getAddress());
        Messenger messenger = proxyFactory.create(Messenger.class, url);
        return messenger.send(msg);
    }
}
